library(foreign)
library(arm)
library(mvtnorm)

# function that recodes variables to range from 0 to 1
recode_01 <- function(x) {
  x <- as.numeric(as.character(x))
  (x - min(x, na.rm=T))/(max(x,na.rm=T) - min(x,na.rm=T))}

# load in CCES and Census data sets
cces <- read.dta("CCES2016.dta")
demos2000 <- read.dbf("Lags.dbf")
demos2015 <- read.dbf("2015 Lags.dbf")
demos2015.a <- read.csv("2011-2015 ACS.csv")
demos2015.b <- read.csv("2011-2015 ACS econ.csv")[,-1]

demos2015 <- demos2015[!duplicated(demos2015[,"zip"]),]
demos2000 <- demos2000[!duplicated(demos2000[,"ZCTA"]),]
demos2015.c <- data.frame(cbind(demos2015.a, demos2015.b))
demos2000$zip <- as.numeric(paste(demos2000$ZCTA))

# merge data sets
demos.m <- merge(demos2000, demos2015, by.x = "zip", by.y = "zip", all.y = T)
demos.m <- merge(demos.m, demos2015.c, by.x = "zip", by.y = "ZIP", all.x = TRUE)
df.m <- merge(cces, demos.m, by.x = "lookupzip", by.y = "zip")

# recode variables
df.m$fb_d <- recode_01(df.m[,"pctfb15"] - df.m[,"pctfb00"])
df.m$fb_d.n <- df.m[,"pctfb15"] - df.m[,"pctfb00"]
df.m$fb_lag <- recode_01(df.m[,"LAG_P15"])
df.m$fb_lag.n <- df.m[,"LAG_P15"]

df.m$white <- as.numeric(df.m$race == "White")
df.m$hisp <- as.numeric(df.m$race == "Hispanic")
df.m$asian <- as.numeric(df.m$race == "Asian")
df.m$black <- as.numeric(df.m$race == "Black")

republican <- as.numeric(df.m$CC16_421a == "Republican")

df.m$trump <- as.numeric(df.m$CC16_410a == "Donald Trump (Republican)")

df.m$trump <- as.numeric(df.m$CC16_410a == "Donald Trump (Republican)")
df.m$trump[which(df.m$CC16_410a == "I didn't vote in this election")] <- NA
df.m$trump[which(df.m$CC16_410a == "Skipped")] <- NA
df.m$trump[which(df.m$CC16_410a == "Not Asked")] <- NA
df.m$trump[which(df.m$CC16_410a == "I'm not sure")] <- NA

df.m$gender <- as.numeric(df.m$gender == "Female")
df.m$educ <- as.numeric(df.m$educ)

df.m$faminc <- as.numeric(df.m$faminc)
df.m$faminc[which(df.m$faminc > 17)] <- NA

df.m$own <- as.numeric(df.m$ownhome == "Own")

df.m$hisp <- as.numeric(df.m$hispanic == "Yes")

df.m$imm <- as.numeric(df.m$immstat != "Immigrant non-citizen")

df.m$pid7 <- NA
df.m$pid7[which(df.m$CC16_421_rep == "Strong Republican")] <- 7
df.m$pid7[which(df.m$CC16_421_rep == "Not so strong Republican")] <- 6
df.m$pid7[which(df.m$CC16_421b == "The Republican Party")] <- 5
df.m$pid7[which(df.m$CC16_421b == "Neither")] <- 4
df.m$pid7[which(df.m$CC16_421b == "Not sure")] <- 4
df.m$pid7[which(df.m$CC16_421b == "The Democratic Party")] <- 3
df.m$pid7[which(df.m$CC16_421_dem == "Not so strong Democrat")] <- 2
df.m$pid7[which(df.m$CC16_421_dem == "Strong Democrat")] <- 1
df.m$pid7[which(is.na(df.m$pid7))] <- 4

df.m$pcthisp_01 <- recode_01(df.m$c_pcthisp0)
df.m$pctblack_01 <- recode_01(df.m$c_pctblack)
df.m$pctunemp_01 <- recode_01(df.m$c_pctunemp)
df.m$pctasian_01 <- recode_01(df.m$c_pctasian)
df.m$medianhsvl_01 <- recode_01(df.m$c_medianho)

imm1 <- as.numeric(df.m$CC16_331_1 == "No")
imm2 <- as.numeric(df.m$CC16_331_2 == "Yes")
imm3 <- as.numeric(df.m$CC16_331_3 == "No")
imm4 <- as.numeric(df.m$CC16_331_7 == "Yes")

imm.scale <- rowMeans(cbind(imm1, imm2, imm3, imm4), na.rm = TRUE)

df.m$lived <- as.numeric(df.m$CC16_361)
df.m$lived[df.m$lived > 7] <- NA

df.m$age <- recode_01(2016 - df.m$birthyr)

df.m$ideology <- as.numeric(df.m$CC16_340a)
df.m$ideology[which(df.m$ideology == 8)] <- 4
df.m$ideology[which(df.m$ideology > 8)] <- NA

